package com.kedll.library.addimg;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kedll.library.R;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.contants.Consts;
import com.kedll.library.listener.OnSetTitleBarVisibleListener;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.Parse;
import com.kedll.library.widget.AbViewPager;

import java.util.ArrayList;
import java.util.Map;


/**
 * @author QiQi
 */
public class ViewImgFragmentActivity01 extends MyBaseFragmentActivity implements OnClickListener, OnSetTitleBarVisibleListener {


    /**
     * 自定义ViewPager
     */
    private AbViewPager ppViewImg;
    /**
     * 底部
     */
    private LinearLayout llBottom;
    /**
     * 确定
     */
    private TextView tvDetermine;

    /**
     * 已选图片
     */
    private ArrayList<Map<String, Object>> selectedList;
    /**
     * 显示当前点击的图片
     */
    private int current;

    @Override
    public void onClick(View v) {
        for (int i = 0; i < selectedList.size(); i++) {
            if (!Parse.getInstance().parseBool(selectedList.get(i).get("isCheck"))) {
                selectedList.remove(i--);
            }
        }
        Intent in = new Intent();
        in.putExtra("selectedList", selectedList);
        in.putExtra("isFinish", true);
        setResult(RESULT_OK, in);
        finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        ppViewImg = findViewById(R.id.pp_view_img);
        llBottom = findViewById(R.id.ll_bottom);
        tvDetermine = findViewById(R.id.tv_determine);

        selectedList = (ArrayList<Map<String, Object>>) getIntent().getSerializableExtra("selectedList");
        current = getIntent().getIntExtra("current", 0);
        if (selectedList != null) {
            for (int i = 0; i < selectedList.size(); i++) {
                if (Parse.getInstance().parseBool(selectedList.get(i).get("isAdd"))) {
                    selectedList.remove(i--);
                }
            }
        } else {
            selectedList = new ArrayList<>();
        }


        setTitle((current + 1) + "/" + (selectedList.size()));
        ppViewImg.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        ppViewImg.setCurrentItem(current);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        ppViewImg.clearOnPageChangeListeners();
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_activity_base_top_bar, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.menu_1).setIcon(ContextCompat.getDrawable(this, R.drawable.selected_img));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_1) {
            if (Parse.getInstance().parseBool(
                    selectedList.get(current).get("isCheck"))) {
                selectedList.get(current).put("isCheck", false);
                item.setIcon(ContextCompat.getDrawable(this, R.drawable.selected_img_false));
            } else {
                selectedList.get(current).put("isCheck", true);
                item.setIcon(ContextCompat.getDrawable(this, R.drawable.selected_img));
            }
//            toolbar.invalidate();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragmentactivity_view_img01;
    }

    @Override
    protected void initEvent() {
        toolbar.setNavigationOnClickListener(v -> {
            for (int i = 0; i < selectedList.size(); i++) {
                if (!Parse.getInstance().parseBool(
                        selectedList.get(i).get("isCheck"))) {
                    selectedList.remove(i--);
                }
            }
            Intent in = new Intent();
            in.putExtra("selectedList", selectedList);
            setResult(RESULT_OK, in);
            finish();
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        });

        ppViewImg.addOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageSelected(int arg0) {
                setTitle((arg0 + 1) + "/" + (selectedList.size()));
                current = arg0;
                MenuItem menuItem = toolbar.getMenu().findItem(R.id.menu_1);
                if (Parse.getInstance().parseBool(selectedList.get(arg0).get("isCheck"))) {
                    menuItem.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.selected_img));
                } else {
                    menuItem.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.selected_img_false));
                }
                toolbar.invalidate();
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        tvDetermine.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            for (int i = 0; i < selectedList.size(); i++) {
                if (!Parse.getInstance().parseBool(selectedList.get(i).get("isCheck"))) {
                    selectedList.remove(i--);
                }
            }
            Intent in = new Intent();
            in.putExtra("selectedList", selectedList);
            setResult(RESULT_OK, in);
            finish();
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置标题栏与底部隐藏与显示
     */
    @Override
    public void setTitleBottomVisibly() {
        if (appBarLayout.getVisibility() == View.VISIBLE) {
            appBarLayout.startAnimation(AnimationUtils.loadAnimation(
                    getApplicationContext(), R.anim.top_out_600));
            appBarLayout.setVisibility(View.GONE);
            llBottom.startAnimation(AnimationUtils.loadAnimation(
                    getApplicationContext(), R.anim.bottom_out_600));
            llBottom.setVisibility(View.GONE);
        } else {
            appBarLayout.setVisibility(View.VISIBLE);
            appBarLayout.startAnimation(AnimationUtils.loadAnimation(
                    getApplicationContext(), R.anim.top_in_600));
            llBottom.setVisibility(View.VISIBLE);
            llBottom.startAnimation(AnimationUtils.loadAnimation(
                    getApplicationContext(), R.anim.bottom_in_600));
        }
    }

    /**
     * ViewPager适配器
     */
    class MyPagerAdapter extends FragmentStatePagerAdapter {

        MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            ViewImgFragment01 vif = new ViewImgFragment01();
            Bundle bundle = new Bundle();
            bundle.putString(
                    "imgURI",
                    "file://"
                            + Parse.getInstance().isNull(
                            selectedList.get(arg0).get("imagePath")));
            bundle.putInt(Consts.ACTIVITY_KEY, 0);
            vif.setArguments(bundle);
            return vif;
        }

        @Override
        public int getCount() {
            return selectedList.size();
        }

    }

}
